
package com.cloud.rapid.screen.util;

import com.cloud.rapid.screen.constant.ResponseCodeEnum;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;


/**
 * 响应信息主体
 *
 * @param <T>
 * @author lengleng
 */
public class ApiResponse<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String msg;

    @Getter
    @Setter
    private T data;

    /**
     * 操作成功
     *
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> ok() {
        return restResult(null, ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getName());
    }

    /**
     * 操作成功
     *
     * @param data 数据
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> ok(T data) {
        return restResult(data, ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getName());
    }

    /**
     * 操作成功
     *
     * @param data 数据
     * @param msg  提示消息
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> ok(T data, String msg) {
        return restResult(data, ResponseCodeEnum.SUCCESS.getCode(), msg);
    }

    /**
     * 操作失败
     *
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> failed() {
        return restResult(null, ResponseCodeEnum.ERROR_FAILED.getCode(), ResponseCodeEnum.ERROR_FAILED.getName());
    }

    /**
     * 操作失败
     *
     * @param msg 提示消息
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> failed(String msg) {
        return restResult(null, ResponseCodeEnum.ERROR_FAILED.getCode(), msg);
    }

    /**
     * 操作失败
     *
     * @param data 数据
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> failed(T data) {
        return restResult(data, ResponseCodeEnum.ERROR_FAILED.getCode(), ResponseCodeEnum.ERROR_FAILED.getName());
    }

    /**
     * 操作失败
     *
     * @param msg  提示消息
     * @param code 错误编码
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> failed(String msg, int code) {
        return restResult(null, code, msg);
    }


    /**
     * 操作失败
     *
     * @param data 数据
     * @param msg  提示消息
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> failed(T data, String msg) {
        return restResult(data, ResponseCodeEnum.ERROR_FAILED.getCode(), msg);
    }


    /**
     * 判断请求结果是否成功
     *
     * @param res
     * @return
     */
    public static boolean isSuccess(ApiResponse res) {
        return res != null && res.getCode() == ResponseCodeEnum.SUCCESS.getCode();
    }

    /**
     * 返回数据
     *
     * @param data 数据
     * @param code 编码
     * @param msg  提示消息
     * @param <T>
     * @return
     */
    private static <T> ApiResponse<T> restResult(T data, int code, String msg) {
        ApiResponse<T> apiResult = new ApiResponse<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    @Override
    public String toString() {
        return "ApiResponse{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}

